/**
 * ParkingDeviceMapper.java 
 * Copyright © 2014-2016, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年5月20日
 */
package com.pshare.artemis.parkingdevice.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.pshare.artemis.parkingdevice.bean.ParkingDevice;
import com.pshare.artemis.parkingdevice.vo.ParkingLockDetailVO;
import com.pshare.framework.mapper.BaseMapper;

/**
 * 停车设施管理持久层
 */
public interface ParkingDeviceMapper extends BaseMapper<Long, ParkingDevice> {

	@Insert("insert into tbl_parking_device (id, name, code,mac,gateway_id) "
			+ "values (#{id},  #{name}, #{code},#{mac},#{gatewayId})")
	int add(ParkingDevice parkingDevice);
	
	@Select("select*from tbl_parking_device where gateway_id=#{gatewayId} ")
	List<ParkingDevice> queryByGateway(@Param("gatewayId")Long gatewayId);
	
	@Select("select*from tbl_parking_device where mac=#{mac} ")
	ParkingDevice getByMac(@Param("mac")String mac);
	
	@Select("SELECT*from tbl_parking_device where gateway_id in(SELECT id from tbl_gateway where user_id=#{userId}) ")
	List<ParkingDevice> getByUserId(@Param("userId")Long userId);
	
	@Update("update tbl_parking_device set deleted=1 where id=#{lockId}")
	void delLock(@Param("lockId")Long lockId);
	
	List<ParkingLockDetailVO> queryItemsByMac(@Param("macs")List<String> macs);

	void clearGatewayNotInMac(@Param("gatewayId")Long gatewayId,@Param("macs")List<String> macs);

	@Update("update tbl_parking_device set code=0 and gateway_id=null where id=#{lockId}")
	void clearGateway(@Param("lockId")Long lockId);
	
	@Update("update tbl_parking_device set is_online=0,update_time='2000-1-1' where gateway_id=#{gatewayId}")
	void setOfflineByGateway(@Param("gatewayId")Long gatewayId);
	
	@Update("update tbl_parking_device set is_online=0,update_time='2000-1-1' where id=#{id}")
	void setOffline(@Param("id")Long id);
	
	@Delete("delete from tbl_parking_device where id = #{id}")
	void delete(@Param("id")Long id);
	@Select("select a.*,b.name user_name from tbl_user b right"
			+ " join (select d.*,g.name gateway_name,g.user_id from tbl_gateway g right join tbl_parking_device d on g.id=d.gateway_id) "
			+ "a on a.user_id=b.id where b.id=#{id}  order by a.id")
	public List<ParkingLockDetailVO> getCMSLocklist(@Param("id")Long id);
	
}
